package com.example.controller.backend;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.common.ApiResult;
import com.example.entity.VisitHistory;
import com.example.service.VisitHistoryService;
import com.example.vo.VisitHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/visitHistory")
@Api(tags = "观看历史")
public class BackendVisitHistoryController {


    @Resource
    private VisitHistoryService visitHistoryService;

    /**
     * 显示全部数据
     * @return
     */

    @PostMapping("/listAll")
    @ApiOperation("查看全部列表")
    public ApiResult<List<VisitHistory>> list(){
        List<VisitHistory> list = visitHistoryService.list();
        return ApiResult.ok(list);
        /*http://localhost:8080/api/visitHistory/listAll*/
    }


    /**
     * 假删除
     * @param id
     * @return
     */
    @DeleteMapping("/realDelete/{id}")
    @ApiOperation("真删除")
    /*   http://localhost:8080/api/visitHistory/realDelete/7     */
    public ApiResult realDelete(@PathVariable Integer id){
        boolean b = visitHistoryService.removeById(id);
        return ApiResult.ok(b);
    }

    @DeleteMapping("/falseDelete")
    @ApiOperation("假删除")
    public ApiResult falseDeleteById( Integer id){
        Integer falseDelete = visitHistoryService.falseDelete(id);
        return ApiResult.ok(falseDelete);
    }


    @GetMapping("/Page/{courseName}/{page}/{size}")
    @ApiOperation("分页查询")
    public ApiResult queryPage(@PathVariable  int page ,
                                 @PathVariable  int size){
        Page<VisitHistory> visitHistoryPage = visitHistoryService.queryPage1(page, size);
        PageDTO<VisitHistory> pageDTO = new PageDTO<>();
        pageDTO.setTotal(visitHistoryPage.getTotal());
        pageDTO.setRecords(visitHistoryPage.getRecords());
        return ApiResult.ok(pageDTO);
    }

    /**
     * 增加数据
     * @param openid
     * @param userId
     * @param videoId
     * @param courseId
     * @param videoLocation
     * @return
     */

    @GetMapping("/add/{openid}/{userId}/{videoId}/{courseId}/{videoLocation}")
    @ApiOperation("增加观看历史")
    public ApiResult add(@PathVariable  String openid,
                         @PathVariable  Integer userId,
                         @PathVariable  Integer videoId,
                         @PathVariable  Integer courseId,
                         @PathVariable  Integer videoLocation) {
        VisitHistory visitHistory = new VisitHistory();
        visitHistory.setOpenid(openid);
        visitHistory.setUserId(userId);
        visitHistory.setCourseId(courseId);
        visitHistory.setVideoId(videoId);
        visitHistory.setVideoLocation(videoLocation);
        boolean save = visitHistoryService.save(visitHistory);
        return ApiResult.ok(save);
        //http://localhost:8080/api/visitHistory/add/"www"/1/1/1/1

    }


    @PutMapping("/update")
    @ApiOperation("修改数据")
    public ApiResult update(@RequestBody VisitHistory visitHistory){
        boolean b = visitHistoryService.updateById(visitHistory);
        return ApiResult.ok(b);
    }













        /*    *//**
         * 查看最新的数据num条。
         * @param num
         * @return
         *//*
    @GetMapping("/findByLimit/{num}")
    @ApiOperation("查看最新的数据num条数据")
    *//*  http://localhost:8080/api/visitHistory/findByLimit/2        *//*
    public ApiResult findByLimit(@PathVariable int num){
        List<VisitHistoryVO> byLimit = visitHistoryService.findByLimit(num);
        return ApiResult.ok(byLimit);
    }*/


        /*

         */


/**
 * 修改数据
 * @param id
 * @param openid
 * @param userId
 * @param videoId
 * @param courseId
 * @param videoLocation
 * @return
 *//*

    @PutMapping("/update/{id}/{openid}/{userId}/{videoId}/{courseId}/{videoLocation}")
    @ApiOperation("修改数据")
    public ApiResult update(@PathVariable Integer id,
                            @PathVariable String openid,
                            @PathVariable Integer userId,
                            @PathVariable Integer videoId,
                            @PathVariable Integer courseId,
                            @PathVariable Integer videoLocation){
        VisitHistory visitHistory = new VisitHistory();
        visitHistory.setId(id);
        visitHistory.setUserId(userId);
        visitHistory.setOpenid(openid);
        visitHistory.setCourseId(courseId);
        visitHistory.setVideoId(videoId);
        visitHistory.setVideoLocation(videoLocation);
        boolean b = visitHistoryService.updateById(visitHistory);
        return ApiResult.ok(b);
    }
*/

}